//
//  XJZPQSearchGoodsViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/4.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  宅配圈搜索商品

#import "XJZPQSearchGoodsViewController.h"
#import "RBGoodsDetailViewController.h"


//model
#import "XJSearchGoodsModel.h"
//view
#import "XJSerachGoodsCell.h"


@interface XJZPQSearchGoodsViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *goodsArray;

@end

@implementation XJZPQSearchGoodsViewController
#pragma mark - 一 lazy
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        NSString *classStr = NSStringFromClass([XJSerachGoodsCell class]);
        
        [_collectionView registerNib:[UINib nibWithNibName:classStr bundle:nil] forCellWithReuseIdentifier:classStr];
        
    }
    return _collectionView;
}

- (NSMutableArray *)goodsArray {
    if (!_goodsArray) {
        _goodsArray = [NSMutableArray array];
    }
    return _goodsArray;
}

#pragma mark - 二 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor redColor];
    
    self.collectionView.frame = self.view.bounds;
    [self.view addSubview:self.collectionView];
}


#pragma mark -  三 -1 UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    
    return self.goodsArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *classStr = NSStringFromClass([XJSerachGoodsCell class]);

    XJSerachGoodsCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:classStr forIndexPath:indexPath];

    cell.model = self.goodsArray[indexPath.row];
    
    return cell;
}

#pragma mark - 三-2 UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((KWidth-8)/2, 200);
}

//lineSpacing 控制的上下间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}

//interItemSpaceing 控制的是左右间距 [] <-----> []
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 2;
}

#pragma mark -  三 -3 UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    XJSearchGoodsModel *goodM = self.goodsArray[indexPath.row];
    XJLog(@"%@",goodM);

    RBGoodsDetailViewController *goodsDetailVC = [RBGoodsDetailViewController new];
    goodsDetailVC.goodsId = goodM.goodsId;
    
    [self.navigationController pushViewController:goodsDetailVC animated:YES];
}



#pragma mark - 网络请求
- (void)requestDataWithKeyWord:(NSString *)keyword {
    
    //    NSString *lat = @"30.194859";
    //    NSString *lng = @"120.189506";
    
    [XJHttpTool requestHomeSearchWithType:@"0" keyWord:keyword lat:@"30.194859" lng:@"120.189506" page:@"1" successH:^(id responseObjc) {
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            
            NSArray *goods = responseObjc[@"data"][@"goods"];
            
            if (goods.count == 0) return ;
            
            for (NSDictionary *tempDic in goods) {
                XJSearchGoodsModel *model = [XJSearchGoodsModel mj_objectWithKeyValues:tempDic];
                
                [self.goodsArray addObject:model];
            }
        }
        

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
        
    } failH:^(NSError *error) {
        XJLog(@"失败:%@",error);
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
